package com.naizhuai.ui.user.login;

import android.view.View;

import com.naizhuai.Model.AccountHelper;
import com.naizhuai.Model.ModelProvider;
import com.naizhuai.Model.db.user.PersonUserBean;
import com.naizhuai.Model.db.user.UserDataSourse;
import com.naizhuai.Model.db.user.UserRepository;
import com.naizhuai.R;
import com.naizhuai.network.NetWorkObserver;
import com.naizhuai.network.apistore.UserManageDataApiStore;
import com.naizhuai.ui.user.UserLogin;
import com.naizhuai.ui.user.UserManageDataApiStoreIml;
import com.naizhuai.util.DialogUtils;
import com.naizhuai.util.ValidateUtil;

import rx.Observer;
import rx.Subscription;

/**
 * @author：lichenxi
 * @date 2018/2/23 12
 * email：525603977@qq.com
 * Fighting
 */
public class LoginPresenter implements LoginContract.Presenter {
    private LoginContract.View mLoginView;
    private UserManageDataApiStore mUserManageDataApiStore;
    private UserRepository mUserRepository;
    private Subscription requestSub;
    public LoginPresenter() {
        mUserManageDataApiStore=UserManageDataApiStoreIml.getInstance();
        mUserRepository= ModelProvider.provideUserRepository();
    }

    @Override
    public void takeView(LoginContract.View view) {
        this.mLoginView=view;
    }

    @Override
    public void dropView() {
        this.mLoginView=null;
        if(requestSub!=null&&!requestSub.isUnsubscribed()){
            requestSub.unsubscribe();
        }
    }

    @Override
    public void userLogin(String phone, String password) {
        mLoginView.showLoading(true);
        requestSub= mUserManageDataApiStore.userLogin(phone,password)
                               .subscribe(new NetWorkObserver<UserLogin>() {
                                   @Override
                                   protected void onFail(String errormsg) {
                                       mLoginView.showLoading(false);
                                       mLoginView.loginFail(errormsg);
                                   }
                                   @Override
                                   protected void onSuccess(UserLogin user) {
                                       if (mLoginView!=null){
                                           mLoginView.showLoading(false);
                                           mLoginView.saveUidSid(user.getUid(), user.getSid());
                                       }
                                       mUserRepository.updateLoginUser(new UserDataSourse.GetUserCallback() {
                                           @Override
                                           public void onUserLoaded(PersonUserBean user) {
                                               mLoginView.onlyTest(user.getPhoneNumber());
                                               AccountHelper.saveComPany(user);
                                               mLoginView.loginSuccess();
                                           }
                                           @Override
                                           public void onDataNotAvailable() {
                                           }
                                       });
                                   }
                               });

    }

    @Override
    public void searchLocalUser(String phone) {
      //dbuser 存在则回调 不存在就gg
        mUserRepository.getUserByPhone(phone, new UserDataSourse.GetUserCallback() {
            @Override
            public void onUserLoaded(PersonUserBean user) {
                mLoginView.userExist(user.getName(),user.getUserLogoUrl());
            }
            @Override
            public void onDataNotAvailable() {

            }
        });

    }

    @Override
    public void cancelRequest() {
        if(requestSub!=null&&!requestSub.isUnsubscribed()){
             requestSub.unsubscribe();
        }
    }
}
